package com.doodle.sharedpreferences;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private int i=0;
    private SharedPreferences pref;
    private SQLiteDatabase db;
    private File sdcardPath;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取临时存储方式的存储位置
        pref = PreferenceManager.getDefaultSharedPreferences(this);

        // 获取系统sdcard的存储位置
        sdcardPath= Environment.getExternalStorageDirectory();

        // 获取SQLite数据库
        //db=SQLiteDatabase.openOrCreateDatabase(sdcardPath.getAbsolutePath() + "zzw.db", null, null);
        dbHelper = new DBHelper(this,sdcardPath.getAbsolutePath()+"/zzw.db",null,1);
        db = dbHelper.getWritableDatabase();

        Log.i("zzw", sdcardPath.getAbsolutePath() + "...............................");
    }

    /*SharedPreference方式存储数据，临时存储数据的方式。*/
    public void writeData(View view) {
        Set<String> set = new HashSet<String>();
        set.add("pingpang");
        set.add("basketball");
        set.add("music");
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("name", "zzw");
        editor.putInt("age", 120);
        editor.putStringSet("fav", set);
        editor.commit();
    }
    public void readData(View view) {
        String name = pref.getString("name", "null");
        int age = pref.getInt("age", 0);
        Set<String> fav = pref.getStringSet("fav",null);
        Log.i("zzw", name + "....." + age);
        for(String key:fav){
            Log.i("zzw",key);
        }
    }

    /* SQLiteDatabase的方式存储数据，安卓的专属存储方式。
     存在的问题就是在应用在重复创建同样的表的时候会有异常的报出。
     解决方式就是引入SQLiteOpenHelper类，将程序创建数据库的操作
     保存在该类的实现子类里。*/
    public void writeDB(View view) {
        String sql="insert into users(name,age) values(?,?)";
        try {
            db.beginTransaction();
            db.execSQL(sql, new Object[]{"ZZW"+i++, 20+(i++)});
            db.setTransactionSuccessful();
        }catch (Exception e){
        }
        finally {
            db.endTransaction();
        }

    }

    public void readDB(View view) {
        String sql="select id _id,name _name,age _age from users where id>?";
        Cursor c = db.rawQuery(sql,new String[]{"2"});
        while(c.moveToNext()){
            int id=c.getInt(c.getColumnIndex("_id"));
            String name=c.getString(c.getColumnIndex("_name"));
            int age=c.getInt(c.getColumnIndex("_age"));
            Log.i("zzw",id+"...."+name+"...."+age);
        }
    }
}
